package com.gt.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.admin.domain.CheckFlow;
import com.gt.admin.domain.CheckGraph;
import com.gt.admin.domain.apiVO.FlowSchedule;
import com.gt.admin.domain.vo.*;
import com.gt.admin.mapper.CheckFlowMapper;
import com.gt.admin.mapper.GraphMapper;
import com.gt.admin.service.ICheckFlowService;
import com.gt.admin.utils.Graph;
import com.gt.admin.utils.GraphChart;
import com.gt.admin.utils.GraphData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckFlowServiceImpl extends ServiceImpl<CheckFlowMapper, CheckFlow> implements ICheckFlowService {
    @Autowired
    private CheckFlowMapper checkFlowMapper;

    @Autowired
    private GraphMapper flowGraphMapper;

    @Override
    public List<CheckFlowVO> list(CheckFlowVO checkFlowVO){
        if (checkFlowVO.getSickId() == null){
            checkFlowVO.setSickId(0L);
        }return checkFlowMapper.list(checkFlowVO);
    }

    /**
     * 进度监控
     * @param checkFlowVO
     * @return
     */
    @Override
    public FlowSchedule schedule(CheckFlowVO checkFlowVO) {
        // 筛查目录id 为空时， 默认为0
        if (checkFlowVO.getSickId() == null){
            checkFlowVO.setSickId(0L);
        }
        // 进度监控
        FlowSchedule flowSchedule = new FlowSchedule();
        flowSchedule.setProjectList(getFlowProject(checkFlowVO, flowSchedule.getScheduleDetailVOList()));
        return flowSchedule;
    }

    /**
     * 获取项目数据，并添加明细数据          待实现
     */
    public List<FlowProject> getFlowProject(CheckFlowVO checkFlowVO,List<FlowScheduleDetailVO> scheduleDetailVOList){
        List<FlowProject> projectList = new ArrayList<>();
        FlowProject project1 = FlowProject.builder()
                .id(1L)
                .projectName("诊断性病例随访")
                .targetNum(439)
                .yearNum(360)
                .flowCaseNum(460)
                .allFlowCaseNum(420)
                .percent("88%")
                .build();
        FlowProject project2 = FlowProject.builder()
                .id(2L)
                .projectName("治疗性病例随访")
                .targetNum(340)
                .yearNum(310)
                .flowCaseNum(382)
                .allFlowCaseNum(367)
                .percent("89.5%")
                .build();
        FlowProject project3 = FlowProject.builder()
                .id(3L)
                .projectName("筛查高风险随访")
                .targetNum(478)
                .yearNum(315)
                .flowCaseNum(400)
                .allFlowCaseNum(365)
                .percent("86.5%")
                .build();
        FlowProject project4 = FlowProject.builder()
                .id(4L)
                .projectName("筛查结果后续随访")
                .targetNum(379)
                .yearNum(320)
                .flowCaseNum(332)
                .allFlowCaseNum(317)
                .percent("85%")
                .build();
        projectList.add(project1);
        projectList.add(project2);
        projectList.add(project3);
        projectList.add(project4);
        for ( FlowProject project : projectList) {
            scheduleDetailVOList.add(getScheduleDetail(checkFlowVO, project.getId()));
        }return projectList;
    }

    /**
     * 获取项目明细数据                  待实现
     */
    public FlowScheduleDetailVO getScheduleDetail(CheckFlowVO checkFlowVO, Long id){
        FlowScheduleDetail scheduleDetail1 = FlowScheduleDetail.builder()
                .flowHospital("遵义医科大学附属医院")
                .targetNum1(230)
                .yearNum1(199)
                .targetNum2(260)
                .yearNum2(234)
                .flowCaseNum(383)
                .allFlowCaseNum(412)
                .percent("89.5%")
                .build();
        FlowScheduleDetail scheduleDetail2 = FlowScheduleDetail.builder()
                .flowHospital("遵义医科大学第二附属医院")
                .targetNum1(213)
                .yearNum1(289)
                .targetNum2(251)
                .yearNum2(213)
                .targetNum3(214)
                .yearNum3(213)
                .flowCaseNum(350)
                .allFlowCaseNum(410)
                .percent("88%")
                .build();
        FlowScheduleDetail scheduleDetail3 = FlowScheduleDetail.builder()
                .flowHospital("遵义市妇幼保健院")
                .targetNum1(200)
                .yearNum1(199)
                .targetNum2(255)
                .yearNum2(233)
                .targetNum3(240)
                .yearNum3(230)
                .flowCaseNum(355)
                .allFlowCaseNum(411)
                .percent("86%")
                .build();
        FlowScheduleDetail scheduleDetail4 = FlowScheduleDetail.builder()
                .flowHospital("遵义市中医医院")
                .targetNum1(211)
                .yearNum1(175)
                .targetNum2(240)
                .yearNum2(230)
                .targetNum3(241)
                .yearNum3(233)
                .flowCaseNum(333)
                .allFlowCaseNum(134)
                .percent("84%")
                .build();
        List<FlowScheduleDetail> scheduleDetailList = new ArrayList<>();
        scheduleDetailList.add(scheduleDetail1);
        scheduleDetailList.add(scheduleDetail2);
        scheduleDetailList.add(scheduleDetail3);
        scheduleDetailList.add(scheduleDetail4);
        FlowScheduleDetailVO scheduleDetailVO = FlowScheduleDetailVO.builder()
                .id(id)
                .scheduleDetailList(scheduleDetailList)
                .build();
        return scheduleDetailVO;
    }

    /**
     * 分析，返回图表数据
     * @param checkFlowVO
     * @return
     */
    @Override
    public List<Graph> analyse(CheckFlowVO checkFlowVO) {
        // 筛查目录id 为空时， 默认为0
        if (checkFlowVO.getSickId() == null){
            checkFlowVO.setSickId(0L);
        }
        // 分析图表
        List<Graph> graphList = new ArrayList<>();
        graphList.add(new Graph(GraphChart.BAR_COL, getGraphDatas(checkFlowVO,GraphChart.BAR_COL)));
        graphList.add(new Graph(GraphChart.BAR_ROW, getGraphDatas(checkFlowVO,GraphChart.BAR_ROW)));
        graphList.add(new Graph(GraphChart.PIE, getGraphDatas(checkFlowVO,GraphChart.PIE)));
        graphList.add(new Graph(GraphChart.BROKEN_LINE, getGraphDatas(checkFlowVO,GraphChart.BROKEN_LINE)));
        return graphList;
    }

    /**
     * 获取表单数据                                        待实现
     * @param checkFlowVO
     * @return
     */
    public List<GraphData> getGraphDatas(CheckFlowVO checkFlowVO,String chart) {
//        GraphData data1 = new GraphData();
//        GraphData data2 = new GraphData();
//        GraphData data3 = new GraphData();
//        GraphData data4 = new GraphData();
        List<GraphData> dataList = new ArrayList<>();

        List<CheckGraph> flowGraphs = flowGraphMapper.selectList(Wrappers.<CheckGraph>lambdaQuery()
                .eq(CheckGraph::getAnalyseModel, checkFlowVO.getAnalyseModel())
                .eq(CheckGraph::getType,GraphChart.FLOW_GRAPH));


        for (CheckGraph flowGraph : flowGraphs) {
            if (chart.equals(flowGraph.getChart())) {
                GraphData data = new GraphData();
                data.setLabel(flowGraph.getXCoordinate());
                data.setValue(flowGraph.getYCoordinate());
                dataList.add(data);
            }
        }
//        data1.setLabel("doctor");
//        data1.setValue(13);
//        data2.setLabel("patient");
//        data2.setValue(66);
//        data3.setLabel("aaa");
//        data3.setValue(4);
//        data4.setLabel("数据");
//        data4.setValue(8);
//        dataList.add(data1);
//        dataList.add(data2);
//        dataList.add(data3);
//        dataList.add(data4);
        return dataList;
    }
}
